/**
 * 删除
 * @param obj
 */
function commonDel(obj) {
    var url = $(obj).attr("data-url");
    var id = $(obj).attr("data-id");
    layer.confirm('确认要删除吗？', function () {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: url,
            data: {'id': id},
            success: function (re) {
                console.log(re);
                if (re.code === 1) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    $(obj).parents("tr").remove();
                } else {
                    layer.alert(re.msg);
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    });
}

/**
 * 接口删除
 * @param url
 * @param id
 * @param obj
 */
function ajaxDel(url, id, obj) {
    layer.confirm('确认要删除吗？', function () {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: url,
            data: {'id': id},
            success: function (re) {
                console.log(re);
                if (re.code === 1) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    $(obj).parents("tr").remove();
                } else {
                    layer.alert(re.msg);
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    });
}

/**
 * 编辑
 * @param obj
 */
function commonEdit(obj) {
    var url = $(obj).attr("data-url");
    var id = $(obj).attr("data-id");
    var title = $(obj).attr("data-title");
    layer.open({
        type: 2,
        shadeClose: true,
        title: title,
        scrollbar: false,
        area: ['100%', '100%'],
        shade: [0],
        fixed: false,
        maxmin: true,
        anim: 3,
        content: [url + '?id=' + id] //iframe的url，no代表不显示滚动条
    });
}

/**
 * 提交表单
 * @returns {boolean}
 */
function commonSubmit(obj) {
    var form = $("#form");
    var formData = new FormData(form[0]);
    console.log(formData);
    $(obj).attr('disabled', true).addClass('disabled');
    var url_ = form.attr('data-url');
    $.ajax({
        url: url_,
        type: 'POST',
        dataType: 'json',
        // data: form.serialize(),
        data:formData,
        processData: false,
        contentType: false,
        cache: false,
        async: false,
        success: function (re) {
            // console.log(re);
            $(obj).removeAttr('disabled', true).removeClass('disabled');
            if (re.code === 100) {
                layer.msg(re.msg, {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒
                }, function () {
                    re.url ? window.location.href = re.url : '';
                });
                //  window.location.href=document.referrer;
            } else {
                layer.msg(re.msg,{ icon: 2,time: 2000});
            }
        }
    });
    return false;
}

/**
 * 关闭layer弹出层
 */
function commonClose() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

/**
 * 简单ajax提交链接
 * @param url
 */
function jumpUrl(url) {
    $.ajax({
        type: 'POST',
        url: url,
        dataType: 'JSON',
        success: function (re) {
            if (re.code === 1) {
                console.log(re);
                layer.msg(re.msg, {icon: 1, time: 2000});
            } else {
                layer.alert(re.msg);
            }
        },
        error: function (data) {
            console.log(data);
        }
    });
}

/**
 * layer打开
 * @param url
 * @param id
 */
function layopen(url, id) {
    layer.open({
        type: 2,
        shadeClose: true,
        scrollbar: false,
        area: ['100%', '100%'],
        shade: [0],
        fixed: false,
        maxmin: true,
        anim: 3,
        content: [url + '?id=' + id] //iframe的url，no代表不显示滚动条
    });
}

/**
 *转换日期格式(时间戳转换为datetime 24小时格式)
 * @param timestamp
 * @returns {string}
 */

function format(timestamp) {
    var d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
    return (d.getFullYear()) + "-" +
        (d.getMonth() + 1) + "-" +
        (d.getDate()) + " " +
        (d.getHours()) + ":" +
        (d.getMinutes()) + ":" +
        (d.getSeconds());
}
/**
 *
 * @param nS
 * @returns {string}
 */
function getLocalTime(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}
//字段过滤 相当于PHP中trim函数
function trimStr(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}